package com.isoft.c2team3service2.handler;

import com.isoft.c2team3service2.bean.ResponseData;
import com.isoft.c2team3service2.bean.ResultCode;
import com.isoft.c2team3service2.util.WebUtils;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Component;

import java.io.IOException;

//针对Security框架403异常
@Component
@Slf4j
public class AccessDenieHandlerImpl implements AccessDeniedHandler {
    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException accessDeniedException) throws IOException, ServletException {
        log.error("AccessDenieHandle:"+accessDeniedException.getClass().getName()+"___"+accessDeniedException.getMessage());
        ResponseData responseData = new ResponseData();
        responseData.setErrorCode(ResultCode.ACCESS_DENIED.getCode());
        responseData.setMsg(ResultCode.ACCESS_DENIED.getMessage());
        //告诉客户端浏览器，返回的是json格式字符串
        WebUtils.renderObj(response,responseData);
    }
}
